Skip to content

Reimpl/map units updatefunctions - #204

Draft
gynt wants to merge 198 commits into
mainfrom
reimpl/map-units-updatefunctions
Draft

gynt wants to merge 198 commits into
mainfrom
reimpl/map-units-updatefunctions

Conversation

@gynt

@gynt gynt commented Aug 24, 2026 •

Copy link
Copy Markdown
Contributor

Turns out these functions are among the longest/hardest. Feel free to add to this PR or fork it.

@gynt
gynt force-pushed the reimpl/map-units-updatefunctions branch from d8ba907 to 609ef6d Compare September 25, 2026 06:52
gynt and others added 26 commits September 25, 2026 09:31
The original indexes the units array with it; short* was a decompiler artifact.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
gynt added a commit to kotijekriv/OpenSHC that referenced this pull request Nov 24, 2026
…#471)

Units::UpdateBatteringRam, from gynt's draft 0ab0162 (52.83%). reccmp reads 100.0, raw and normalized, in a
narrower build of main 25d2e9e with this commit: the files of main's /GL list, the callees the visibility
manifest names and the unit (both resolver sets on). Against main alone with main's list, the other functions
read as before.

cmake/compiler-flags-gl.txt gets two lines: the unit and UnitsState/deselectUnit.cpp (0x00535190, at 100% on
main). The unit has to see deselectUnit's body: in the loop that sends the four engineers to work the original
reads the engineer's owner through edx behind deselectUnit (0x00545F7A) and calls clearOrDeselectUnitFromSelection
without loading ecx again (0x00545F85). hasUnitReachedDestination, which it also has to see, is on main's list.
cmake/function-visibility.txt gets an entry for the unit with these two callees, at its address.

What the code takes from the original's listing:

- engineersInPlace is declared first: the original copies the owner into displayColorPlayerID through ax
  (0x005459FB) and clears the counter behind it (0x00545A15); declared behind the owner copies, the counter made
  our compiler use dx for the copy.
- The loop that checks whether the AI's four engineers are back leaves by three separate tests, and the scan for
  engineers skips by six separate tests. The original keeps a zero in edx across both loops (made again behind the
  first at 0x00545BCE and on the second's back edge at 0x00545D30); written as one chain, either loop loses the
  match (quick_diff 0.9105 and 0.9448 against 0.9884).
- The search for the capturing owner is a loop from 1 to 9 with a found flag: the original tests first, counts to
  9 and returns behind the loop (0x00545E50 to 0x00545E5D).
- createTribe gets the owner of units[searched], with searched the counter the scan left behind (imul ebp at
  0x00545EBA); sourcehold#204's draft passes the ram's new owner. After the scan searched is maxUnitCount: most likely a
  bug in the original, kept for the match and marked fixme.
- addUnitToTribe gets the new tribe first and the ram second, as in the original (0x00545EDA / 0x00545EDB),
  although it takes (unitID, tribeID): most likely a second bug of the original, marked fixme like the first.
- Every access reads the unit through DAT_CurrentUnitSlotID::instance, except the state store behind
  stopUnitIfNextToTarget (0x00534240, not reimplemented), which goes through the copy made at the start: the
  original stores through esi there (0x005464D3).

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
6and8 pushed a commit to 6and8/OpenSHC that referenced this pull request Nov 24, 2026
…rlay, with UnitState names (100%) (sourcehold#409)

* header: UnitState gets US_UNKNOWN_117, 202, 203, 204, 205 and 209

Units::UpdateDeer (0x00541DE0) and Units::UpdateRabbit (0x00542630) dispatch on the unit's state through jump
tables that run to 209 (cmp ecx, 0xd1 at 0x00541E5F with tables at 0x00542558 and 0x00542528; cmp eax, 0xd1 at
0x0054276E with tables at 0x00542C34 and 0x00542C0C). Deer's table has its own entries for 117, 202 to 205 and
209, Rabbit's for 117, 203, 204 and 209 (202 and 205 go to its default). They store these states (mov word ptr
[reg + 0x138880C]: the units array at 0x0138854C plus state at 0x2C0): Deer 205, 203 and 204 at 0x00541EBC,
0x00541ED5 and 0x00541EE0 and 117 at 0x0054244B and 0x0054249C, Rabbit 203, 204 and 117 at 0x005427C6,
0x005427D3 and 0x00542B0E, and TribesState::scatterTribeUnitsRandomly stores 209 at 0x00523A0A. The enum goes
from US_STONE_DEATH_03 (116) to US_UNKNOWN_120 and ends at US_ASSASSIN_CLIMBING_DOWN (129); the six are named in
the US_UNKNOWN_<n> form. Used in place of (UnitState)N and the numeric case labels; code generation is the same.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* reimplement: SHC_3BB0A8C1_0x0053A020 100.0% Reimplemented

Units::CurrentUnitHasHealer, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions, the branch of
PR sourcehold#204 (6d8ad88, 89.29%, "loop precheck scheduling"; the file last changed in 974c32f; 92.9 on main's headers
in the 43a17df baseline). The current player is read into a const local, currentPlayer, before the loop, as the
original loads currentPlayerSlotID before the loop's precheck, the way PlayArmoryIsFullWarning and
WarnIfPlayersGranaryIsFull read theirs (read in the condition: quick_diff 0.9259 -> 0.8519). maxUnitCount is an
int, so the (int) cast goes, and the loop counts unitID as main's unit loops do. reccmp 100.0%.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* reimplement: SHC_3BB0A8C1_0x00542630 100.0% Reimplemented

Units::UpdateRabbit, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (209f6d4 at 69.32%,
49bd9c9 at 79.00%):
- units[] is indexed through unitID or through DAT_CurrentUnitSlotID::instance per statement, as a greedy
  quick_diff search picked them one statement per step;
- the US_UNKNOWN_203 path sits inside the `> 0` test, the slowdown is an int compare with (fixedRng & 0xf), and
  the death cases test animationFrame <= 0 first, the original's branch directions (structural_diff.py);
- tribe is an int local, so its load is movsx as in the original; it is read at the top, as reading it where it
  is used loses the match (quick_diff 1.0000 -> 0.5216);
- when hasUnitReachedDestination returns TRUE in the movement case, animationCycleNumber and substate are written
  through a local Unit pointer. The scheduler (Schedule, called from dop2_PreEncode) cannot tell those two stores
  apart from the state.generic store through units[unitID] and gives each a memory edge to it, so they come before
  pop edi and pop edi before the third store, as in the original; with all three through units[unitID] the stores
  are independent and pop edi went first. Found with cdb: tuple_dump.py shows the block in source order at
  dop2_PreEncode entry and reordered when it returns, and a breakpoint on DagMakeDep lists the edges;
- states 117, 203, 204 and 209 use the US_UNKNOWN_<n> names of the header commit.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* reimplement: SHC_3BB0A8C1_0x00541DE0 100.0% Reimplemented

Units::UpdateDeer, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (f7d126f at 36.18%,
6458bca at 64.64%, cases in jump-table order):
- units[] is indexed through unitID or through DAT_CurrentUnitSlotID::instance per statement, as a greedy
  quick_diff search picked them one statement per step; in states 204 and 205 the two forms were picked over
  every combination: 204 tests substate through the slot, as 203 does, so its substate is not kept in a register
  for the increment and the two states share one idle tail as in the original;
- when hasUnitReachedDestination returns TRUE, animationCycleNumber and substate are written through a local Unit
  pointer, so the two stores stay ahead of the epilogue's pop edi as in the original (the same lever as in
  UpdateRabbit);
- in both seated branches of state 205 the frame-0 path sets DAT_UnitHasBecomeIdle and falls through to the shared
  idle block instead of repeating it and returning: the original has that block copied into each path;
- frameOffset is declared at the top: the original holds its 1 in edx from the prologue, where it also adds it to
  deerCount (declared in the movement case: quick_diff 0.8300); tribe is read at the top too (just before the
  switch: 0.9122);
- states 117, 202 to 205 and 209 use the US_UNKNOWN_<n> names of the header commit.

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* reimplement: SHC_3BB0A8C1_0x005416B0 100.0% Reimplemented

Units::UpdateMerchant, from Gynt's draft on sourcehold's reimpl/map-units-updatefunctions (0adb264 at 53.39%,
8372c08 at 63.80%), under /GL:
- every call that takes the unit passes DAT_CurrentUnitSlotID::instance, and units[] is indexed through unitID or
  through the slot per statement, as a greedy quick_diff search picked them one statement per step;
- in state 3 the three stores after the destination test go through the slot, which the original reads again
  after setDestinationForUnit, and gfxNumber and field_0x30_animRelated are written through a local Unit pointer,
  so they come after the stateBasedSpeed store as in the original;
- states 2 and 3 are US_STATE_2Unk and US_STATE_3Unk, and state 2's fade step is written as -2 (the byte the
  original stores, 0xFE; same code);
- compiler-flags-gl.txt lists UpdateMerchant.cpp: 100% with the line, 78.0% without it. The original keeps values
  in registers across checkIfCitizenUnitIsAliveBasedOnState and hasUnitReachedDestination, which main's list
  already has, and reads DAT_CurrentUnitSlotID again after activateTraderState, which stays off the list: with its
  body visible the slot stays in esi across the call (90.0% instead of 100%).

Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* reimplement: SHC_3BB0A8C1_0x004BC790 100.0% Reimplemented

UI::Rendering::RenderAnimatedTutorialFloatOverlay, from 0287fe5 on reimpl/batch1-development, in the form
TheRedDaemon worked out in his review: x and y step down by 8 and 32 before the per-resolution offset, and by 28
and 69 after the draw buffer is saved, and the offset comes from a chain of plain ifs. The original pushes the
trailing 0 before it computes the other arguments, and this form gives that order; one subtraction of 36 and 101
loses the match (quick_diff 0.8035 in the first place, 0.6356 in the second), so the two steps stay, with a
comment. The 1680x1050 offset is the original's 4050, where every other offset is the screen height - 600; a fixme
says so.

The function is Gynt's: 5236e98 (60.79%) and 63cc49f on his fork's reimpl/UI-Rendering-development, and
e8e27aa (60.8%) on reimpl/UI-Rendering (his sourcehold#255); ce6bd1e took it to 67.2% on reimpl/batch1-development.

Co-authored-by: TheRedDaemon <66257843+TheRedDaemon@users.noreply.github.com>
Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Gynt <gynt@users.noreply.github.com>
Co-authored-by: TheRedDaemon <66257843+TheRedDaemon@users.noreply.github.com>
This was referenced Nov 28, 2026
gynt and others added 11 commits December 5, 2026 06:25
The 78 conflicting unit-update files take main's version, which matches
equally well or better; their status lines follow main.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
The headers from main rename the animation frame tables and a few Unit,
PlayerData and ClimbLogicDefinedData fields, and model the per-player
attack info and the pathfinding cost layers that the drafts read
through raw offsets. Use main's names, and read the danger and
tolerance values as UpdatePikeman does.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
Their siblings on main (UpdateFireEater, UpdatePikeman, UpdatePriest and
others) are /GL objects: the original keeps the unit ID and the unit
offset in volatile registers across hasUnitReachedDestination and the
other UnitsState callees, which only LTCG reproduces. With /GL and the
callees in the build list, every draft but UpdateCow gains.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
… chain

The states form one else-if chain whose arms fall out instead of
returning, the unit is read through DAT_CurrentUnitSlotID, and the
roosting case clears the building and joins the shared tail, so the
original's tail call to nextRandomNumber2 is shared too.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
…eEater's form

The juggler is the fire eater with two more acts, so it now follows
UpdateFireEater: one state chain whose arms fall out, the unit read
through DAT_CurrentUnitSlotID, and the same tail sharing between the act
states.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
…chain

Arms fall out of one else-if chain and the unit is read through
DAT_CurrentUnitSlotID, as in UpdateMiller.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
…it through DAT_CurrentUnitSlotID

Index the unit array with DAT_CurrentUnitSlotID instead of a local copy,
as the matching unit updates on main do; LTCG then keeps the original's
registers for more of the function.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
… unit through DAT_CurrentUnitSlotID

Index the unit array with DAT_CurrentUnitSlotID instead of a local copy,
as the matching unit updates on main do; LTCG then keeps the original's
registers for more of the function.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
…through DAT_CurrentUnitSlotID

Index the unit array with DAT_CurrentUnitSlotID instead of a local copy,
as the matching unit updates on main do; LTCG then keeps the original's
registers for more of the function.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
…hrough DAT_CurrentUnitSlotID

Index the unit array with DAT_CurrentUnitSlotID instead of a local copy,
as the matching unit updates on main do; LTCG then keeps the original's
registers for more of the function.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp
…through DAT_CurrentUnitSlotID

Index the unit array with DAT_CurrentUnitSlotID instead of a local copy,
as the matching unit updates on main do; LTCG then keeps the original's
registers for more of the function.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XmXCmXCVBvESeaU1djQFjp

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant